package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.*;
import com.cloudlead.commonData.vo.*;
import com.cloudlead.fleetAction.service.PlacePriceEditSer;
import com.cloudlead.fleetService.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by wyj on 2015/8/17.
 */
@Service
public class PlacePriceEditSerImpl implements PlacePriceEditSer {
    @Autowired
    TbPlacePriceService tbPlacePriceService;
    @Autowired
    BaseDockService baseDockService;
    @Autowired
    BaseDoorService baseDoorService;
    @Autowired
    RegionService regionService;
    @Autowired
    BaseCompanyService  baseCompanyService;
    @Autowired
    TbDictionaryService tbDictionaryService;

    @Override
    public String createPlacePrice(PlacePriceCreateVo cvo) {
        TbPlacePrice tpp = new TbPlacePrice();
        BeanUtils.copyProperties(cvo, tpp);//拷贝类型相同元素
        /**
         * 不同类型元素处理
         */
        if(cvo.getBase_place()!=null&&(!"".equals(cvo.getBase_place()))){//区域
            Region re = new Region();
                re.setCode(cvo.getBase_place());
            tpp.setBasePlace(regionService.findOne(re));
        }
        if(cvo.getDoo()!=null&&(!"".equals(cvo.getDoo()))){//门点
            Region re = new Region();
                re.setCode(cvo.getDoo());
            tpp.setDoo(regionService.findOne(re));
        }
        if(cvo.getBaseDock()!=null&&(!"".equals(cvo.getBaseDock()))){//码头
            BaseDock bd = new BaseDock();
                bd.setId(cvo.getBaseDock());
            tpp.setBaseDock(bd);
        }
        if(cvo.getBaseCompany()!=null&&(!"".equals(cvo.getBaseCompany()))){//公司
            BaseCompany bc = new BaseCompany();
                bc.setId(cvo.getBaseCompany());
            tpp.setBaseCompany(bc);
        }
        tpp.setFlag("0");//0表示未删除
        tbPlacePriceService.create(tpp);
        return "success";
    }

    @Override
    @Transactional
    public Page<PlacePriceVo> findAllPlacePrice(PlacePriceVo vo,int size,int page) {

        PageRequest pr = new PageRequest();
        List<Term> tr=new ArrayList<Term>();
        tr.add(new Term("flag","0"));//未删除
        //查询条件
        if(vo!=null) {
            if(vo.getId()!=null&&(!"".equals(vo.getId()))){
                tr.add(new Term("id",vo.getId()));
            }
            //公司
            if (vo.getBaseCompany() != null && (!vo.getBaseCompany().equals(""))) {
                tr.add(new Term("baseCompanyId", vo.getBaseCompany()));
            }
            //码头
            if (vo.getBaseDock() != null && (!vo.getBaseDock().equals(""))) {
                tr.add(new Term("baseDockId", vo.getBaseDock()));
            }
            //门点
            if (vo.getDoor5() != null && vo.getDoor5()!=0) {
                tr.add(new Term("doo.code", vo.getDoor5()));
            }
            //区域
            if (vo.getBase_place() != null && vo.getBase_place()!=0) {
                tr.add(new Term("base_place.code", vo.getBase_place()));
            }
        }


        Filter filter=new Filter( new Criteria( tr));
           // Sort sort=new Sort();
           // sort.add(new Sort.Order(Sort.Direction.ASC,"id"));//id排序
            pr.setPage(page);//页码
            pr.setSize(size);//每页数量
            pr.setFilter(filter);
            //pr.setSort(sort);//排序
        Page<TbPlacePrice> ppp = tbPlacePriceService.findAll(pr);
        List<TbPlacePrice> ltp=ppp.getContent();//得到page中内容
        List<TbPlacePrice> lpvo = new ArrayList<TbPlacePrice>();
        Page<PlacePriceVo> ppv= new Page<PlacePriceVo>();

        for(Iterator<TbPlacePrice> it = ltp.iterator();it.hasNext();) {//遍历
            TbPlacePrice t =it.next();
                if(t.getBaseDock()!=null)
                    t.setBaseDock(baseDockService.view(t.getBaseDock().getId()));//码头
                if(t.getBasePlace()!=null)
                    t.setBasePlace(regionService.view(t.getBasePlace().getId()));//区域
                if(t.getDoo()!=null)
                    t.setDoo(regionService.view(t.getDoo().getId()));//门点
            lpvo.add(t);
        }
        ppp.setContent(lpvo);
        BeanUtils.copyProperties(ppp, ppv);//拷贝page
        return ppv;
    }

    @Override
    public String delPlacePrice(String id) {
        TbPlacePrice tpp=tbPlacePriceService.view(id);
        if(tpp!=null){//查询到记录
            tpp.setFlag("1");//flag=1表示删除
            tbPlacePriceService.update(tpp);
        }else
            return "error ID";
        return "success";
    }
}
